package com.etc.user.controller;

import com.etc.dao.FlashOrderDao;
import com.etc.entity.FlashOrder;
import com.etc.result.CodeMsg;
import com.etc.result.Result;
import com.etc.service.FlashOrderService;
import com.etc.vo.OrderInfoMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名:com.etc.user.controller.FlashOrderController
 * 描述：秒杀订单的控制器
 * 作者:吴仕泉
 * 时间:2019/5/28 2:31
 */
@Controller
@RequestMapping("flashorder.action")
public class FlashOrderController {

    @Autowired
    FlashOrderService flashOrderService;
    @Autowired
    FlashOrderDao flashOrderDao;

    //订单详情页的展示
    @RequestMapping("showorderinfo")
    @ResponseBody
    public Result<OrderInfoMessage> showOrderInfo(String orderNum){
       // System.out.println("orderNum:"+orderNum);
        if(orderNum.length()!=22 ){

            FlashOrder fo = flashOrderDao.selectByPrimaryKey(Integer.parseInt(orderNum));
            orderNum = fo.getFlashordernum();
        }
        if(orderNum==null || orderNum ==""|| orderNum.length()!=22 ){

            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }


        OrderInfoMessage oim = flashOrderService.findOrderInfo(orderNum);

        if(oim == null){
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }

        List<OrderInfoMessage> list = new ArrayList<>();
        list.add(oim);
        return Result.success(list);
    }

    //展示用户的所有秒杀订单信息
    @RequestMapping("showuserallorderinfo")
    @ResponseBody
    public Result<OrderInfoMessage> showUserAllFlashOrderInfo(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid){
        System.out.println("uid:"+uid);
        if(uid==null  || uid<=0){
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
        List<OrderInfoMessage> oims = flashOrderService.findUserAllFlashInfo(uid);
        if(oims == null){
            Result.error(CodeMsg.ORDER_NOT_EXIST);
        }
        return Result.success(oims);
    }

    //对订单进行付款操作，订单状态修改为1
    @RequestMapping("updateuserpay")
    @ResponseBody
    public Result<Integer> updateUserPay(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid,@RequestParam(value = "foid" ,defaultValue = "-1") Integer foid) {
        System.out.println(uid+":::"+foid);
        if(uid<=0&&foid<=0){
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
        boolean flag = flashOrderService.updateOrderState(foid,1);
        if(flag){
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return Result.success(list);
        }
        return Result.error(CodeMsg.ORDER_PAY_ERROR);
    }
    //对订单进行发货操作，订单状态修改为2
    @RequestMapping("updateadmindeliver")
    @ResponseBody
    public Result<Integer> updateAdminDeliver(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid,@RequestParam(value = "foid" ,defaultValue = "-1") Integer foid) {
       if(uid<=0&&foid<=0){
           return Result.error(CodeMsg.REQUEST_ILLEGAL);
       }
        boolean flag = flashOrderService.updateOrderState(foid,2);
        if(flag){
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return Result.success(list);
        }
        return Result.error(CodeMsg.ORDER_OPERATION_ERROR);
    }

    //用户对订单进行确认收货操作，订单状态修改为3
    @RequestMapping("updateusersurereceipt")
    @ResponseBody
    public Result<Integer> updateUserSureReceipt(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid,@RequestParam(value = "foid" ,defaultValue = "-1") Integer foid) {
        if(uid<=0&&foid<=0){
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
        boolean flag = flashOrderService.updateOrderState(foid,3);
        if(flag){
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return Result.success(list);
        }
        return Result.error(CodeMsg.ORDER_OPERATION_ERROR);

    }

    //对用户删除秒杀订单操作，订单状态修改为-1
    @RequestMapping("deleteorderuser")
    @ResponseBody
    public Result<Integer> deleteOrderUser(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid,@RequestParam(value = "foid" ,defaultValue = "-1") Integer foid) {
        if(uid<=0&&foid<=0){
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
        boolean flag = flashOrderService.updateOrderState(foid,-1);
        if(flag){
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return Result.success(list);
        }
        return Result.error(CodeMsg.ORDER_OPERATION_ERROR);
    }

    //管理员删除订单操作,订单状态修改为-2
    @RequestMapping("deleteorderadmin")
    @ResponseBody
    public Result<Integer> deleteOrderAdmin(@RequestParam(value = "uid" ,defaultValue = "-1") Integer uid,@RequestParam(value = "foid" ,defaultValue = "-1") Integer foid) {
        if(uid<=0&&foid<=0){
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
        boolean flag = flashOrderService.updateOrderState(foid,-2);
        if(flag){
            List<Integer> list = new ArrayList<>();
            list.add(0);
            return Result.success(list);
        }
        return Result.error(CodeMsg.ORDER_OPERATION_ERROR);
    }

}
